package net.tv90.bfmccore.task;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import static net.tv90.bfmccore.BFMCCore.PLUGIN_ROOT;
import static net.tv90.bfmccore.util.ZipTool.toZip;

public class AutoBackupTask implements Runnable {

    long time = 0L;

    //ZIP输出目标文件夹
    public static File generateZipFile() {
        if (!PLUGIN_ROOT.exists()) PLUGIN_ROOT.mkdir();
        return new File(PLUGIN_ROOT, "data_backup");
    }

    //要压缩的目标文件夹
    public static File generateFolderFile() {
        if (!PLUGIN_ROOT.exists()) PLUGIN_ROOT.mkdir();
        return new File(PLUGIN_ROOT, "data");
    }

    @Override
    public void run() {
        time++;
        //每12小时自动备份一次
        int timeInterval = 12 * 60 * 60 * 20;
        if(time == timeInterval){
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
            String format = dateFormat.format(date);

            String fileFolderPath = generateFolderFile().getPath();
            String zipPath = generateZipFile().getPath() + "\\data_" + format + ".zip";
            System.out.println("FileFolder_Path = " + fileFolderPath);
            System.out.println("ZIP_Path = " + zipPath);

            toZip(zipPath, fileFolderPath,false);

            time = 0L;

        }
    }
}